<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/21
 * Time: 16:50
 */

namespace app\common\model;

use think\Cache;

/**
 * 商品相关模型
 * Class Goods
 * @package app\common\model
 */
class Goods extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_goods';

    private $acg_gids;
    private $day_gids;
    private $month_gids;
    private $year_gids;
    private $video_gids;
    const GOODS_REC = 'goods_rec';//校准生辰商品缓存键名
    const GOODS_DAY = 'goods_day';//日运商品缓存键名
    const GOODS_MONTH = 'goods_month';//月运商品缓存键名
    const GOODS_YEAR = 'goods_year';//年运商品缓存键名
    const GOODS_MAP = 'goods_acg';//幸运之城商品缓存键名
    const GOODS_VIDEOS = 'goods:videos';//视频商品缓存键名
    const GOODS_NATAL_REPORT = 'goods:natal_report';//本命预测商品缓存键名
    const CACHE_TIME = 86400 * 7;//缓存有效期

    public function initialize()
    {
        parent::initialize();
        $goodsConfig = config('goods');
        $this->acg_gids = $goodsConfig['acg_id'];
        $this->day_gids = $goodsConfig['day_id'];
        $this->month_gids = $goodsConfig['month_id'];
        $this->year_gids = $goodsConfig['year_id'];
        $this->video_gids = $goodsConfig['video_id'];
    }

    /**
     * 获取教学视频的相关商品信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getVideoInfo($map = [])
    {
        $id = $map['id'];
        if (!in_array($id, $this->video_gids))
        {
            $this->error = '当前商品号非视频商品信息';

            return false;
        }

        $map['id'] = ['in', $this->video_gids];

        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::GOODS_VIDEOS);
        }
        if ($data === false)
        {
            try
            {
                $data = $this->field('id,name,or_prize,now_prize,discount,extend')
                    ->where('status', config('code.status_normal'))
                    ->where($map)
                    ->order('sort', 'DESC')
                    ->selectOrFail();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_VIDEOS, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '教学视频商品信息获取失败';

                return false;
            }
        }

        foreach ($data as $k => $v)
        {
            $extend = json_decode($v['extend'], true);
            if (isset($extend['goods_des']) && $extend['goods_des'])
            {
                $data[$k]['goods_des'] = $extend['goods_des'];
            } else
            {
                $data[$k]['goods_des'] = '';
            }
            unset($data[$k]['extend']);
        }
        return $data;
    }

    /**
     * 获取校准生辰商品
     * @param array $map
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     */
    public function getGoodsRec($map = [])
    {
        $id = $map['id'];
        if ($id != 1)
        {
            $this->error = '当前商品号非校准生辰商品信息';

            return false;
        }

        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::GOODS_REC);
        }

        if ($data === false)
        {
            try
            {
                $data = $this
                    ->field('id,name,or_prize,now_prize')
                    ->where('status', config('code.status_normal'))
                    ->findOrFail($map['id'])->toArray();

                Cache::set(self::GOODS_REC, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '获取校准生辰商品信息失败';

                return false;
            }
        }

        return $data;
    }

    /**
     * 获取幸运之城商品
     * @return bool|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getGoodsForACG()
    {
        $map['id'] = ['in', $this->acg_gids];

        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::GOODS_MAP);
        }

        if ($data === false)
        {
            try
            {
                $data = $this->field('id,name,or_prize,now_prize,discount')
                    ->where('status', config('code.status_normal'))
                    ->where($map)
                    ->order('sort', 'DESC')
                    ->selectOrFail();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_MAP, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '获取幸运之城商品信息失败';
                return false;
            }

        }
        return $data;
    }

    /**
     * 获取日运商品
     * @param array $map
     * @return bool|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getGoodsForDay($map = [])
    {
        $id = $map['id'];
        if (!in_array($id, $this->day_gids))
        {
            $this->error = '当前商品号非日运商品信息';

            return false;
        }

        $map['id'] = ['in', $this->day_gids];

        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::GOODS_DAY);
        }

        if ($data === false)
        {
            try
            {
                $data = $this->field('id,name,or_prize,now_prize,discount,expire_time,extend')
                    ->where('status', config('code.status_normal'))
                    ->where($map)
                    ->order('sort', 'DESC')
                    ->selectOrFail();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_DAY, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '日运商品信息获取失败';
                return false;
            }
        }

        if (!empty($data))
        {
            foreach ($data as $k => $v)
            {
                $extend = json_decode($v['extend'], true);
                if (isset($extend['goods_des']) && $extend['goods_des'])
                {
                    $data[$k]['goods_des'] = $extend['goods_des'];
                } else
                {
                    $data[$k]['goods_des'] = '';
                }
                unset($data[$k]['extend']);
            }
        }

        return $data;
    }

    /**
     * 获取月运商品信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getGoodsForMonth($map = [])
    {
        $id = $map['id'];
        if (!in_array($id, $this->month_gids))
        {
            $this->error = '当前商品号非月运商品信息';

            return false;
        }

        $map['id'] = ['in', $this->month_gids];

        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::GOODS_MONTH);
        }
        if ($data === false)
        {
            try
            {
                $data = $this->field('id,name,or_prize,now_prize,discount,expire_time,extend')
                    ->where('status', config('code.status_normal'))
                    ->where($map)
                    ->order('sort', 'DESC')
                    ->selectOrFail();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_MONTH, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '月运商品信息获取失败';
                return false;
            }
        }

        if (!empty($data))
        {
            foreach ($data as $k => $v)
            {
                $extend = json_decode($v['extend'], true);
                if (isset($extend['goods_des']) && $extend['goods_des'])
                {
                    $data[$k]['goods_des'] = $extend['goods_des'];
                } else
                {
                    $data[$k]['goods_des'] = '';
                }
                unset($data[$k]['extend']);
            }
        }
        return $data;
    }

    /**
     * 获取年运商品信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getGoodsForYear($map = [])
    {
        $id = $map['id'];
        if (!in_array($id, $this->year_gids))
        {
            $this->error = '当前商品号非年运商品信息';

            return false;
        }

        $map['id'] = ['in', $this->year_gids];

        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::GOODS_YEAR);
        }
        if ($data === false)
        {
            try
            {
                $data = $this->field('id,name,or_prize,now_prize,discount,expire_time,extend')
                    ->where('status', config('code.status_normal'))
                    ->where($map)
                    ->order('sort', 'DESC')
                    ->selectOrFail();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_YEAR, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '年运商品信息获取失败';
                return false;
            }
        }

        if (!empty($data))
        {
            foreach ($data as $k => $v)
            {
                $extend = json_decode($v['extend'], true);
                if (isset($extend['goods_des']) && $extend['goods_des'])
                {
                    $data[$k]['goods_des'] = $extend['goods_des'];
                } else
                {
                    $data[$k]['goods_des'] = '';
                }
                unset($data[$k]['extend']);
            }
        }
        return $data;
    }

    public function getNatalReport()
    {
        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::GOODS_NATAL_REPORT);
        }
        if ($data === false)
        {
            try
            {
                $data = $this->field('id,name,or_prize,now_prize,extend')
                    ->where(['type' => 5, 'status' => 1])
                    ->order('sort', 'DESC')
                    ->selectOrFail();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::GOODS_NATAL_REPORT, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                $this->error = '一生报告信息获取失败';
                return false;
            }
        }
        return $data;
    }

}
